/*
 * Compares the distance between 2 similar words, like
 * 	levenshtein("Sun", "Sunday") => 3
 *	Here, "Sun" needs 3 characters to make it into "Sunday".
 * Featured on blog homepage on 2/17/2009 
 * http://webreflection.blogspot.com/
 * 
 * Implemented in BJSpell:
 * http://code.google.com/p/bjspell/
 */
var levenshtein = function(min, split){
    // Levenshtein Algorithm Revisited - WebReflection
    try{("0")[0]}catch(i){split=true};
    return function(a, b){
        if(a == b)return 0;
        if(!a.length || !b.length)return b.length || a.length;
        if(split){a = a.split("");b = b.split("")};
        var len1 = a.length + 1,
            len2 = b.length + 1,
            I = 0,
            i = 0,
            d = [[0]],
            c, j, J;
        while(++i < len2)
            d[0][i] = i;
        i = 0;
        while(++i < len1){
            J = j = 0;
            c = a[I];
            d[i] = [i];
            while(++j < len2){
                d[i][j] = min(d[I][j] + 1, d[i][J] + 1, d[I][J] + (c != b[J]));
                ++J;
            };
            ++I;
        };
        return d[len1 - 1][len2 - 1];
    }
}(Math.min, false);